package file;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.net.Socket;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class FileClient extends JFrame implements Runnable{

	String path;
	Socket s;
	
	private JPanel contentPane;
	private JTextField textField;
	private JButton btnNewButton;
	private JButton btnNewButton_1;
	private JScrollPane scrollPane;
	private JTextArea textArea;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					FileClient frame = new FileClient();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public FileClient() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 548, 222);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		contentPane.add(getTextField());
		contentPane.add(getBtnNewButton());
		contentPane.add(getBtnNewButton_1());
		contentPane.add(getScrollPane());
	}
	
	public void run(){
		try{
			s = new Socket("192.168.0.6",5555);
			DataInputStream dis = new DataInputStream(s.getInputStream());
			
			String filename = dis.readUTF();
			File f = new File(path +"//"+ filename);
			textArea.append(path+filename);
			
			textArea.append("파일 받아올거야\n");
			FileOutputStream fos = new FileOutputStream(f);
			
			byte[] b = new byte[1024];
			int n = 0;  
			long filesize = 0; 
			while((n=dis.read(b)) != -1){  //배열의 크기만큼 읽어라. 그 크기만큼 적는다. 10 byte 만큼 저장되있을 때, n에는 10이 저장된다.
				fos.write(b, 0, n); }
			fos.close();
			dis.close();
			s.close();
			textArea.append("다 받아따~~");
			
		}catch(Exception ex){
			ex.printStackTrace();
		}
		
		
	}
	

	private JTextField getTextField() {
		if (textField == null) {
			textField = new JTextField();
			textField.setBounds(12, 10, 288, 21);
			textField.setColumns(10);
		}
		return textField;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("시작");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					
					
					
					
					new Thread(FileClient.this).start();
					
				
				}
			});
			btnNewButton.setBounds(421, 9, 97, 23);
		}
		return btnNewButton;
	}
	private JButton getBtnNewButton_1() {
		if (btnNewButton_1 == null) {
			btnNewButton_1 = new JButton("선택");
			btnNewButton_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					
					JFileChooser jfc = new JFileChooser();
					jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
					int yn = jfc.showOpenDialog(FileClient.this);
					if(yn == JFileChooser.APPROVE_OPTION){
						path = jfc.getSelectedFile().getPath();
					}
					textField.setText(path);
					textArea.append("경로 설정 완료\n");
					
					
				}
			});
			btnNewButton_1.setBounds(312, 9, 97, 23);
		}
		return btnNewButton_1;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBounds(12, 41, 506, 132);
			scrollPane.setViewportView(getTextArea());
		}
		return scrollPane;
	}
	private JTextArea getTextArea() {
		if (textArea == null) {
			textArea = new JTextArea();
		}
		return textArea;
	}
}
